package com.wondertek.mam.tag.service;

import com.wondertek.mam.tag.domain.TagWord;
import com.wondertek.mam.tag.tagrep.TagRep;
import org.apdplat.word.segmentation.Word;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
 * 标签处理类
 * Created by 王东旭 on 2017-09-06 上午 10:30:35.
 */
@Component
public class TagWordProcessor {

    private static final Logger LOGGER = LoggerFactory.getLogger(TagWordProcessor.class);
    private TagRep hotRep;

    public TagWordProcessor(TagRep hotRep){
        this.hotRep = hotRep;
    }

    /**
     * 过滤分词结果得到合格的词语
     * @param words 词语集合
     * @return 合格词汇
     */
    public Collection<TagWord> filterTag(Collection<Word> words){
        List<String> wordsList = new ArrayList<>();
        if (words==null) return new ArrayList<>();
        words.forEach(word -> wordsList.add(word.getText()));
        LOGGER.debug("文本中的合格词汇："+words);
        Collection<TagWord> tagWordWithScore = hotRep.getTagWordWithScore(wordsList);
        LOGGER.debug("匹配库词汇后的结果和热度："+tagWordWithScore);
        return tagWordWithScore;
    }
}
